package algorithm.leetcode.array;

public class SearchMatrix {
    //先找在哪一行，在行里边二分查找
    public boolean searchMatrix(int[][] matrix, int target) {
        //行数
        int rowNum = matrix.length;
        //列数
        int colNum = matrix[0].length;

        for(int i=0; i<rowNum; i++) {
            int left = 0;
            int right = colNum - 1;
            while(left >= 0 && right >=0 && left <= right) {
                int middle = (left + right) / 2;
                if(matrix[i][middle] == target) {
                    return true;
                }

                if(matrix[i][middle] < target) {
                    left = middle + 1;
                }

                if(matrix[i][middle] > target) {
                    right = middle - 1;
                }
            }
        }
        return false;
    }



    public static void main(String[] args) {
        int[][] matrix = new int[][]{{1,4,7,11,15},{2,5,8,12,19},{3,6,9,16,22},{10,13,14,17,24},{18,21,23,26,30}};
        System.out.println(new SearchMatrix().searchMatrix(matrix, 5));
    }
}
